<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of MainMenu
 *
 * @author Teodor
 */
class MainMenu extends CWidget{
    
    public $models;
    
    public $parentId;
    
    public $labele;


    public function init() {
      
    }
    
    public function run() {
        $this->_renderContent();
    }
    
    protected function _buildList(&$models, $parentId){      
        $childrenCatalogs = _getChildren($models, $parentId);
        if(count($childrenCatalogs)>0)
        {
             echo CHtml::openTag('ul', $parentId==0?array('id'=>'nemuBar'):array() );   
             foreach ($childrenCatalogs as $childrenCatalog)
             {       
                 echo CHtml::openTag('li') . CHtml::link($childrenCatalog->name, array('item/index', 'catalogId'=>$childrenCatalog->id));
                 _buildList($models, $childrenCatalog->id);      
                 echo CHtml::closeTag('li');
             }
             echo CHtml::closeTag('ul');
        }
     }

   protected function _getChildren(&$models, $parentId)
    {
        return array_filter($models, function ($e) use (&$parentId) {
            return $e[$this->parentId]==$parentId;
        });
    }
    
    protected function _buildMenu()
    {
        $mainItems = $this->_getChildren($this->models, 0);
        echo CHtml::openTag("ul", array("id"=>"menu"));
        foreach ($mainItems as $item){
           echo CHtml::openTag('li');
                echo CHtml::link($item[$this->labele], "#"); 
                echo CHtml::openTag("div", array("class"=>"drop"));
                     echo "Contemt";//ToDo: Build Blocks of items
                echo CHtml::closeTag('div');//drop element
           echo CHtml::closeTag("li");
        }
        echo CHtml::closeTag("ul"); //Menu
    }
    
    protected function _renderContent(){        
       // _buildList($this->models, 0); 
       $this->_buildMenu(); 
        
    }
}
